﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// 中间件基类
    /// </summary>
    public abstract class MiddlewareAbs : IMiddleware
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MiddlewareAbs()
        {

        }

        /// <summary>
        /// 尝试调用中间件处理[已处理返回true,否则返回false]
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response">http响应</param>
        /// <returns>已处理返回true,否则返回false</returns>
        public bool TryInvoke(HttpRequest request, out IHttpResponse response)
        {
            return this.PrimitiveTryInvoke(request, out response);
        }

        /// <summary>
        /// 尝试调用中间件处理[已处理返回true,否则返回false]
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response">http响应</param>
        /// <returns>已处理返回true,否则返回false</returns>
        protected abstract bool PrimitiveTryInvoke(HttpRequest request, out IHttpResponse response);



        /// <summary>
        /// 当前对象是否已释放标识[true:已释放;false:未释放]
        /// </summary>
        protected bool _disposed = false;
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (this._disposed)
                {
                    return;
                }
                this._disposed = true;

                this.Dispose(true);
            }
            catch (Exception ex)
            {
                ZLoger.Error(ex);
            }
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing">disposing</param>
        protected virtual void Dispose(bool disposing)
        {

        }
    }
}
